Skip to content

feat(ai-openrouter): forward system-prompt cache_control breakpoints#777

Merged
tombeckenham merged 3 commits into
TanStack:mainfrom
lecstor:fix/openrouter-system-prompt-cache-control
Jun 24, 2026
Merged

feat(ai-openrouter): forward system-prompt cache_control breakpoints#777
tombeckenham merged 3 commits into
TanStack:mainfrom
lecstor:fix/openrouter-system-prompt-cache-control

Conversation

@lecstor

@lecstor lecstor commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

@tanstack/ai-openrouter's text adapter collapsed systemPrompts to a plain joined string and dropped the object-form metadata, so Anthropic-family prompt caching over OpenRouter was unreachable — a cache_control breakpoint never reached the wire. (OpenAI routes auto-cache prefixes, so only Anthropic-family routes were affected.)

This mirrors what @tanstack/ai-anthropic already does:

  • Declare OpenRouterSystemPromptMetadata ({ cache_control?: ChatContentCacheControl }) and thread it through BaseTextAdapter's TSystemPromptMetadata param, so cache_control is typed and autocompleted at the chat() call site.
  • In mapOptionsToRequest, when any system prompt carries cache_control, emit the system message as a ChatContentText[] content array with cacheControl on the part(s) that declared it. Without cache_control, the system message is sent as the same joined string as before — existing callers are unaffected.
chat({
  adapter: openRouterText('anthropic/claude-sonnet-4.5'),
  systemPrompts: [
    {
      content: 'Large, stable instructions — cache me.',
      metadata: { cache_control: { type: 'ephemeral' } },
    },
    'Volatile per-request instruction.',
  ],
})

Tests cover: a single cached prompt (asserting both the camelCase pre-serialization shape and the snake_case cache_control wire output via ChatRequest$outboundSchema), mixed cached/uncached prompts (breakpoint only on the prompt that declared it), and the unchanged no-cache_control path (foreign metadata still dropped, plain string preserved).

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features
    • System prompts can now include an optional per-prompt cache_control directive, which is forwarded to OpenRouter on supported routes.
    • Added a new public metadata type to correctly describe systemPrompts entries that carry cache control.
  • Bug Fixes
    • Fixed system-prompt caching for Anthropic-family models by preserving and forwarding per-prompt cache directives instead of dropping them.
  • Tests
    • Updated and expanded test coverage to validate correct serialization and that only the relevant prompt includes cache_control.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 34f7f8e1-8389-4805-9a05-3d79760f6f77

📥 Commits

Reviewing files that changed from the base of the PR and between 0b7f36a and afce2fe.

📒 Files selected for processing (1)
  • packages/ai-openrouter/tests/openrouter-adapter.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/ai-openrouter/tests/openrouter-adapter.test.ts

📝 Walkthrough

Walkthrough

Adds per-system-prompt cache_control forwarding in OpenRouterTextAdapter. A new exported OpenRouterSystemPromptMetadata type with an optional cache_control field is introduced, and mapOptionsToRequest now emits structured system content when needed while preserving the existing joined-string path otherwise.

Changes

OpenRouter system prompt cache_control support

Layer / File(s) Summary
OpenRouterSystemPromptMetadata type and public export
packages/ai-openrouter/src/text/text-provider-options.ts, packages/ai-openrouter/src/index.ts
Adds ChatContentCacheControl import, defines and documents the exported OpenRouterSystemPromptMetadata type with an optional cache_control field, and re-exports it from the package index.
Adapter type wiring and mapOptionsToRequest
packages/ai-openrouter/src/adapters/text.ts
Updates OpenRouterTextAdapter's BaseTextAdapter type argument to OpenRouterSystemPromptMetadata, adds ChatContentText import, and branches mapOptionsToRequest to emit a structured content array with per-prompt cacheControl when any prompt declares cache_control, otherwise falls back to the existing joined-string path.
Tests and changeset
packages/ai-openrouter/tests/openrouter-adapter.test.ts, .changeset/openrouter-system-prompt-cache-control.md
Updates the existing mixed-metadata test to use foreign metadata with an explicit drop assertion. Adds two new tests: one verifying structured content emission for a single cached prompt and one verifying cache_control is applied only to the declaring prompt while others become plain text parts. Records a minor version changeset.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • TanStack/ai#575: Introduced the normalized { content, metadata } system prompt shape whose metadata field is now narrowed to OpenRouterSystemPromptMetadata to carry cache_control through to the wire request.

Suggested reviewers

  • AlemTuzlak
  • tombeckenham

Poem

🐇 Hop hop, the cache is set!
The prompts now keep their tidy debt.
With cache_control in the stream,
The system sings a structured dream.
Soft ears twitch: “All set!”

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: forwarding system-prompt cache_control breakpoints in ai-openrouter.
Description check ✅ Passed The description follows the template with Changes, Checklist, and Release Impact sections filled in and relevant implementation details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

The text adapter collapsed `systemPrompts` to a plain joined string and
dropped the object-form `metadata`, so Anthropic-family prompt caching over
OpenRouter was unreachable — `cache_control` never reached the wire.

Declare `OpenRouterSystemPromptMetadata` (threaded through `BaseTextAdapter`'s
`TSystemPromptMetadata`, so `cache_control` is typed/autocompleted at the
`chat()` call site) and, when any system prompt carries `cache_control`, emit
the system message as a content-array part carrying the directive — mirroring
`@tanstack/ai-anthropic`. Without `cache_control` the system message is still
the same joined string, so existing callers are unaffected.
@lecstor lecstor force-pushed the fix/openrouter-system-prompt-cache-control branch from e121468 to 6eccb78 Compare June 18, 2026 02:36
@nx-cloud

nx-cloud Bot commented Jun 24, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 0b7f36a

Command Status Duration Result
nx run-many --targets=build --exclude=examples/... ✅ Succeeded 6s View ↗

☁️ Nx Cloud last updated this comment at 2026-06-24 07:51:26 UTC

@tombeckenham tombeckenham merged commit 170451d into TanStack:main Jun 24, 2026
3 checks passed

@tombeckenham tombeckenham left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this

@github-actions github-actions Bot mentioned this pull request Jun 24, 2026
@lecstor

lecstor commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for this

@tombeckenham thank you! (and the rest of the team 😄)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants